#include "cxwizardpage.h"
#include "ui_cxwizardpage.h"
#include <QMessageBox>
#include <boost/xpressive/xpressive.hpp>
#include <iostream>
using namespace std;

CxWizardPage::CxWizardPage(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::CxWizardPage)
{
    ui->setupUi(this);
}

CxWizardPage::~CxWizardPage()
{
    delete ui;
}
bool CxWizardPage::isIPAddr(const QString str)
{
    boost::xpressive::cregex re=boost::xpressive::cregex::compile("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\." \
                                                                  "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\." \
                                                                  "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\."   \
                                                                  "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");

  if(!boost::xpressive::regex_match(str.toStdString().c_str(),re))
   return false;
   else return true;
}

bool    CxWizardPage::validatePage ()
{
    QString ipaddr[4];
    short   port[4];
    ipaddr[0]=ui->lineEdit_ip1->text();
        if(!isIPAddr(ipaddr[0]))goto _FALSE;

    ipaddr[1]=ui->lineEdit_ip2->text();
        if(!isIPAddr(ipaddr[1]))goto _FALSE;

    ipaddr[2]=ui->lineEdit_ip3->text();
        if(!isIPAddr(ipaddr[2]))goto _FALSE;

    ipaddr[3]=ui->lineEdit_ip4->text();
        if(!isIPAddr(ipaddr[3]))goto _FALSE;

    if(ui->lineEdit_port1->text()=="")goto _FALSE;
    port[0]=ui->lineEdit_port1->text().toShort();

    if(ui->lineEdit_port2->text()=="")goto _FALSE;
    port[1]=ui->lineEdit_port1->text().toShort();

    if(ui->lineEdit_port3->text()=="")goto _FALSE;
    port[2]=ui->lineEdit_port1->text().toShort();

    if(ui->lineEdit_port4->text()=="")goto _FALSE;
    port[3]=ui->lineEdit_port1->text().toShort();


    for(int i=0;i<4;++i)
    {
        m_ipAddr[i]=ipaddr[i];
        m_portNo[i]=port[i];
    }
    return true;

_FALSE:
        QMessageBox::warning(this,"Error","Please Input data..");
        return false;


}
